/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mwh
 */
public class VocabularyTableGrep implements VocabularyTable {

    String filename;


    public VocabularyTableGrep(String filename) {
        this.filename = filename;
    }


    public String get(int id) {
        String res = "UNK";
        try {
            String[] tokens = Grep.grepFirst("^" + id + "\\t", filename).split("\t");
            res = tokens[1];

        } catch (FileNotFoundException ex) {
            Logger.getLogger(VocabularyTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(VocabularyTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }


    public int get(String word) {
        int res = -1;
        try {
            String[] tokens = Grep.grepFirst("\\t" + word + "\\t", filename).split("\t");
            res = Integer.parseInt(tokens[0]);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(VocabularyTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(VocabularyTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
}
